package com.open.capacity.firebird.util;

import com.open.capacity.common.model.DB;
import com.open.capacity.common.web.Result;

import java.io.File;
import java.sql.*;

public class FireBirdDriver {
    public static Result firebirdConn(DB db) {
        try {
            //创建文件路径
            File file = new File(db.getDbUrl());
            //判断该文件路径是否存在
            if (!file.exists())
                return Result.failed("文件路径不存在");
            //获取数据库文件路径
            String str = db.getDbUrl();
            //判断是否为.fdb格式的数据库文件
            String suffix = str.substring(str.lastIndexOf("."));
            if (!suffix.equals(".fdb"))
                return Result.failed("抱歉，目前只支持fdb格式的数据库文件");
            String Driver = "org.firebirdsql.jdbc.FBDriver";
            String URL = "jdbc:firebirdsql://localhost:3050/" + db.getDbUrl() + "?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&useAffectedRows=true";
            System.out.println(URL);
            String Username = db.getDbUsername();
            String Password = db.getDbPassword();
            Connection con;
            Class.forName(Driver);
            con = DriverManager.getConnection(URL, Username, Password);
            return con == null ? Result.failed("连接失败") : Result.succeed("连接成功");
        } catch (Exception e) {
            return Result.failed("用户名或密码错误");
        }
    }

}
